<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Shape;
use App\Default_field;
use App\Custom_field;
use App\Category;
class shapeController extends Controller
{
    static $tableName='shapes';
    /**
    * 软删除Trait
    */
    use \App\Http\utils\trashedMethods;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyWord=$request->input('keyWord');
        $is_trashed=$request->input('trashed');
        if($is_trashed){
            $pageListData=Shape::where("shape_name","like","%".$keyWord."%")->onlyTrashed()->orderByDesc('updated_at')->paginate(8);
        }else{
            $pageListData=Shape::where("shape_name","like","%".$keyWord."%")->orderByDesc('updated_at')->paginate(8);
        }
        return view('admin.shape.index',compact('pageListData','is_trashed','keyWord'));
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $fieldTypes=include app_path().'/Http/utils/fieldType.php';
        $defaultFields=Default_field::all();
        return view('admin.shape.form',compact('defaultFields','fieldTypes'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $schema=[
            'shape_name'=>'required',
        ];
        $request->validate($schema);

        
        $createData=$request->only(['shape_name','default_ids','custom_ids']);
        $shape=Shape::create($createData);

        // 处理custorm_arr
        $custom_ids=[];
        $custom_arr=$request->input('custom_arr');
        if($custom_arr){
             $custom_arr=json_decode($custom_arr,true);
            foreach ($custom_arr as $key=>$v) {
                $custom_arr[$key]['shape_id']=$shape->id;
                $custom_field=Custom_field::create($custom_arr[$key]);
                $custom_ids[]=$custom_field->id;
            }
        }
        //把新循环创建的自定义标签的id插入到当前形状中
        $shape->custom_ids=join(',',$custom_ids);
        $shape->save();
        return json(200,'创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $fieldTypes=include app_path().'/Http/utils/fieldType.php';
        $defaultFields=Default_field::all();
        $editData=Shape::find($id);
        $editData['custom_items']=Custom_field::whereIn('id',explode(',',$editData->custom_ids))->get();
        return view('admin.shape.form',compact('editData','defaultFields','fieldTypes'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $schema=[
            'shape_name'=>'required',
        ];
        $request->validate($schema);

        // 处理custorm_arr
        $custom_ids=[];
        $custom_arr=$request->input('custom_arr');

        //因为是更新操作形状已存在，所以先操作/更新/创建 自定义字段数组
        if($custom_arr){
             $custom_arr=json_decode($custom_arr,true);
            foreach ($custom_arr as $key=>$v) {
                //查找字段是更新还是新增
                if(isset($v['id'])){
                    $custom_field=Custom_field::where('id',$v['id'])->update($custom_arr[$key]);
                    $custom_ids[]=$v['id'];
                }else{
                    $custom_arr[$key]['shape_id']=$id;
                    $custom_field=Custom_field::create($custom_arr[$key]);
                    $custom_ids[]=$custom_field->id;
                }
           
            }
        }

        $updateData=$request->only(['shape_name','default_ids','custom_ids']);
        
        //把新循环创建的自定义标签的id插入到当前形状中
        $updateData['custom_ids']=join(',',$custom_ids);
        $shape=Shape::find($id)->update($updateData);

        return json(200,'修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //形状是否被使用
        $is_has=Category::where('shape_id',$id)->exists();
        if($is_has){
            return json(105,'删除失败,模型正在被使用');
        }
        $res=Shape::find($id)->delete();
        if($res) return json(200,'删除成功');
        return json(105,'删除失败');
    }
}
